package com.hmall.item.web;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.item.pojo.Item;
import com.hmall.item.pojo.PageDTO;
import com.hmall.item.service.IItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/item")
public class ItemController {

    @Autowired
    private IItemService itemService;


    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/list")
    public PageDTO<Item> list(@RequestParam Integer page,@RequestParam Integer size){
        /*Page<Item> itemPage = new Page<>((page-1)*size,size);
        PageDTO result = itemService.pagelist(itemPage);
        return result;*/

            Page<Item> page1 = itemService.page(new Page<>(page, size));
            return new PageDTO<>(page1.getTotal(),page1.getRecords());

    }


    @GetMapping("/{id}")
    public Item getById(@PathVariable Long id){
        Item byId = itemService.getById(id);
        return byId;
    }

    @PostMapping
    public void save(@RequestBody Item item){
        itemService.save(item);
    }


    @PutMapping("/status/{id}/{status}")
    public void updateStatus(@PathVariable Long id,@PathVariable Integer status){

        itemService.updateStatus(id,status);
//            itemService.update(Wrappers.<Item>lambdaUpdate().eq(Item::getId,id).set(Item::getStatus,status));

    }

    @PutMapping
    public void updateById(@RequestBody Item item){
        itemService.updateById(item);
    }

    @DeleteMapping("{id}")
    public void deleteById(@PathVariable Integer id){
        itemService.removeById(id);
    }

    /**
     * 扣减库存
     * @param itemId
     * @param num
     */
    @PutMapping("/stock/{itemId}/{num}")
    public void deductStock(@PathVariable("itemId") Long itemId,@PathVariable("num") Integer num){
        itemService.deductStock(itemId,num);
    }

}
